#include "customtoast.h"

#include <QLabel>
#include <QHBoxLayout>
#include <QScreen>
#include <QApplication>
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
#include <QGraphicsOpacityEffect>
#include <QDebug>

CustomToast::CustomToast(QWidget *parent) : QWidget(parent) {

    this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint
                         | Qt::Tool | Qt::WindowStaysOnTopHint);
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    this->setAttribute(Qt::WA_DeleteOnClose, true);

    m_labelContext = new QLabel(this);
    m_labelContext->setText("This is a toast");
    m_labelContext->setAlignment(Qt::AlignCenter);

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->addWidget(m_labelContext);
    this->setLayout(layout);

    m_align = CenterTop;
    m_duration = 500;
    m_fontSize = 28;
    m_fontColor = QColor("black");
    m_backgroundColor = QColor("white");
    m_customX = 200;
    m_customStartY = 400;
    m_customEndY = 200;
}


CustomToast::~CustomToast(){
    qDebug() << "MyToast::~MyToast======= line=== " << __LINE__;
}




void CustomToast::setText(const QString &text)
{
    m_labelContext->setText(text);
}

void CustomToast::setFontColor(const QColor &color)
{
    m_fontColor = color;
}

void CustomToast::setFontSize(int px)
{
    m_fontSize = px;
}

void CustomToast::setBackgroundColor(const QColor &color)
{
    m_backgroundColor = color;
}

void CustomToast::setAlignment(const CustomToast::Alignment &align)
{
    m_align = align;
}

void CustomToast::setDuration(int msec)
{
    m_duration = msec;
}

void CustomToast::setCustomAlign(int x, int startY, int endY)
{
    m_customX = x;
    m_customStartY = startY;
    m_customEndY = endY;
}

void CustomToast::showEvent(QShowEvent *event)
{
    Q_UNUSED(event)

    // 设置样式
    m_labelContext->setStyleSheet(QString(R"(
                                  background-color: %1;
                                  border: none;
                                  border-radius: 5px;
                                  color: %2;
                                  font-size: %3px;
                                  )").arg(m_backgroundColor.name(QColor::HexRgb))
                                      .arg(m_fontColor.name(QColor::HexRgb))
                                      .arg(m_fontSize));

    // 获取屏幕大小，计算toast显示位置
    QScreen *screen = QApplication::screens().at(0);
    int screenWidth = screen->availableGeometry().width();
    int screenHeight = screen->availableGeometry().height();

    // 调整toast大小
    this->adjustSize();
    int toastWidth = this->width() > 300 ? this->width() : 300;
    int toastHeight = this->height() > 48 ? this->height() : 48;
    this->setFixedSize(toastWidth + 40, toastHeight + 20);

    int toastX = (screenWidth - toastWidth) / 2;
    int toastStartY = 0;
    int toastEndY = 0;
    switch (m_align) {
    case Center:
        toastStartY = (screenHeight - toastHeight) / 2;
        toastEndY = screenHeight / 3 + 10;
        break;

    case CenterTop:
        toastStartY = (screenHeight - toastHeight) / 2 - screenHeight / 3;
        toastEndY = 10;
        break;

    case Custom:
        toastX = m_customX;
        toastStartY = m_customStartY;
        toastEndY = m_customEndY;
        break;

    default:
        break;
    }
    this->move(toastX, toastStartY);

    // toast上移动画
    QPropertyAnimation *posAnima = new QPropertyAnimation(this);
    posAnima->setTargetObject(this);
    posAnima->setPropertyName("pos");
    posAnima->setDuration(m_duration);
    posAnima->setStartValue(QPoint(toastX, toastStartY));
    posAnima->setEndValue(QPoint(toastX, toastEndY));

    // toast透明动画
    QGraphicsOpacityEffect *opacityEffect = new QGraphicsOpacityEffect(this);
    opacityEffect->setOpacity(1.0);
    m_labelContext->setGraphicsEffect(opacityEffect);

    QPropertyAnimation *opaAnima = new QPropertyAnimation(opacityEffect, "opacity", this);
    opaAnima->setDuration(m_duration);
    opaAnima->setStartValue(1);
    opaAnima->setEndValue(0.2);

    QParallelAnimationGroup *anGroup = new QParallelAnimationGroup(this);
    anGroup->addAnimation(posAnima);
    anGroup->addAnimation(opaAnima);
    connect(anGroup, &QAbstractAnimation::stateChanged, this,
            [=](QAbstractAnimation::State newState, QAbstractAnimation::State oldState) {
                Q_UNUSED(oldState)
                if (newState == QAbstractAnimation::Stopped) {
                    this->close();
                }
            });
    anGroup->start(QAbstractAnimation::DeleteWhenStopped);
}
